package alipay.openplatform.edu.campus.jiuye.service.services.business;


import alipay.openplatform.edu.campus.jiuye.common.utils.CopyVoPo;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.business.BllUserInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.BllUserInfoPO;
import alipay.openplatform.edu.campus.jiuye.model.vo.BllUserInfoVO;
import alipay.openplatform.edu.campus.jiuye.model.vo.ContextVo;
import alipay.openplatform.edu.campus.jiuye.service.iservice.business.IBllUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@Service
public class BllUserInfoService implements IBllUserInfoService {

    private static final Logger logger = LoggerFactory.getLogger(BllUserInfoService.class);

    @Autowired
    private BllUserInfoMapper bllUserInfoMapper;

    /**
     * 登录
     *
     * @param bllUserInfoVO
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public BllUserInfoVO selectByUserNameAndPassWord(BllUserInfoVO bllUserInfoVO) throws InvocationTargetException, IllegalAccessException {

        BllUserInfoPO bllUserInfoPO = new BllUserInfoPO();

        CopyVoPo.beanUtilsBean.copyProperties(bllUserInfoPO, bllUserInfoVO);
        logger.info("登录|将vo转为po|BllUserInfoService|selectByUserNameAndPassWord");

        bllUserInfoPO = bllUserInfoMapper.selectByUserNameAndPassWord(bllUserInfoPO);
        logger.info("登录|访问MySQL,根据用户名、密码查找记录");

        if (ObjectUtils.isNotNull(bllUserInfoPO)) {
            CopyVoPo.beanUtilsBean.copyProperties(bllUserInfoVO, bllUserInfoPO);
            logger.info("登录成功|将po转为vo|BllUserInfoService|selectByUserNameAndPassWord|参数为:bllUserInfoVO={},bllUserInfoPO={}", bllUserInfoVO, bllUserInfoPO);
            return bllUserInfoVO;
        }
        return null;
    }
    /**
     * 清除登录次数信息
     * @param map
     * @param user_name
     */
    public void removeSession(Map<String, ContextVo> map,String user_name ){
        if (map.get(user_name).getRecoverTime().getTime() < System.currentTimeMillis()) {
            String sessionId=map.get(user_name).toString();
            map.remove(user_name);
            if (logger.isInfoEnabled()) {
                logger.info("ticket[" + sessionId + "]过期已删除！");
            }
        }
    }
}